package com.kehutong.auth.action;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.kehutong.common.util.Token;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.util.Objects;

import com.kehutong.common.DoveClient;

/**
 * 小程序聚合接口
 *
 * @author: liukunlong
 * @date: 2021-04-08 16:42
 */
@MvcAction
@ReqMapping("/kht-bin/aggregate")
public class AggregateAction {

    static final Logger logger = LoggerFactory.getLogger(AggregateAction.class);

    @Inject
    private DoveClient doveClient;

    /**
     * 用户房屋列表接口
     *
     * @param jsonObject 查询条件
     * @return 住户房屋绑定列表
     * @throws Exception 异常
     */
    @ReqMapping("/house/list")
    public Object getResidentHouseList(Token token,JSONObject jsonObject) throws Exception {
        JSONArray result = new JSONArray();
        JSONArray khtIds = jsonObject.getJSONArray("khtIds");
        jsonObject.remove("token");
        if (Objects.nonEmpty(khtIds)) {
            for (Object khtId : khtIds) {
                String companyNo = (String) khtId;
                JSONObject residentHouseResult = doveClient.post("/crm/resident/service/getResidentHouseList", (http) -> {
                    http.addHeader("companyNo", companyNo);
                    jsonObject.put("pageSize",2000);
                    http.setBody(jsonObject.toJSONString());
                });
                if (residentHouseResult.getIntValue("code") == 200) {
                    JSONArray residentHouseList = residentHouseResult.getJSONObject("data").getJSONArray("list");
                    result.addAll(residentHouseList);
                }
            }
        }
        return result;
    }


    /**
     * 用户房屋列表接口
     *
     * @param jsonObject 查询条件
     * @return 住户房屋绑定列表
     * @throws Exception 异常
     */
    @ReqMapping("/house/bind/apply/list")
    public Object getResidentHouseBindApplyList(Token token,JSONObject jsonObject) throws Exception {
        JSONArray result = new JSONArray();
        JSONArray khtIds = jsonObject.getJSONArray("khtIds");
        jsonObject.remove("token");
        if (Objects.nonEmpty(khtIds)) {
            for (Object khtId : khtIds) {
                String companyNo = (String) khtId;
                JSONObject residentHouseBindApplyResult = doveClient.post("/crm/resident/house/bind/service/list", (http) -> {
                    http.addHeader("companyNo", companyNo);
                    jsonObject.put("pageSize",2000);
                    http.setBody(jsonObject.toJSONString());
                });
                if (residentHouseBindApplyResult.getIntValue("code") == 200) {
                    JSONArray residentHouseBindApplyList = residentHouseBindApplyResult.getJSONObject("data").getJSONArray("list");
                    result.addAll(residentHouseBindApplyList);
                }
            }
        }
         return result;
    }


    /**
     * 住户房屋分组列表
     *
     * @param jsonObject 查询条件
     * @return 住户房屋绑定列表
     * @throws Exception 异常
     */
    @ReqMapping("/resident/house/group/list")
    public Object getResidentHouseGroupList(Token token, JSONObject jsonObject) throws Exception {
        List<Map<String, Object>> result = new ArrayList<>();
        JSONArray aggregateResult = new JSONArray();
        JSONArray khtIds = jsonObject.getJSONArray("khtIds");
        jsonObject.remove("token");
        if (Objects.nonEmpty(khtIds)) {
            for (Object khtId : khtIds) {
                String companyNo = (String) khtId;
                JSONObject residentHouseResult = doveClient.post("/crm/resident/service/getResidentHouseList", (http) -> {
                    http.addHeader("companyNo", companyNo);
                    jsonObject.put("pageSize",2000);
                    http.setBody(jsonObject.toJSONString());
                });
                if (residentHouseResult.getIntValue("code") == 200) {
                    JSONArray residentHouseList = residentHouseResult.getJSONObject("data").getJSONArray("list");
                    aggregateResult.addAll(residentHouseList);
                }
            }
        }
        Map<String, Map<String, Object>> communityMapping = new LinkedHashMap<>();
        //小区编号与小区下面房屋的映射
        Map<String, List<Map<String, Object>>> mapping = new LinkedHashMap<>();
        if (Objects.nonEmpty(aggregateResult)) {
            for (Object residentHouseObj : aggregateResult) {
                JSONObject residentHouse = (JSONObject) residentHouseObj;
                String id = residentHouse.getString("id");
                String communityNo = residentHouse.getString("communityNo");
                String communityName = residentHouse.getString("communityName");
                String communityViewName = residentHouse.getString("communityViewName");
                String buildNo = residentHouse.getString("buildNo");
                String buildName = residentHouse.getString("buildName");
                String buildViewName = residentHouse.getString("buildViewName");
                String floorName = residentHouse.getString("floorName");
                String unitName = residentHouse.getString("unitName");
                String houseNo = residentHouse.getString("houseNo");
                String houseName = residentHouse.getString("houseName");
                String houseViewName = residentHouse.getString("houseViewName");
                String companyNo = residentHouse.getString("companyNo");
                JSONObject type = residentHouse.getJSONObject("type");
                JSONObject houseType = residentHouse.getJSONObject("houseType");
                String avatar = residentHouse.getString("avatar");
                String realname = residentHouse.getString("realname");
                String mobile = residentHouse.getString("mobile");

                Map<String, Object> houseMap = new LinkedHashMap<>();
                houseMap.put("id", id);
                houseMap.put("buildNo", buildNo);
                houseMap.put("buildName", buildName);
                houseMap.put("buildViewName", buildViewName);
                houseMap.put("floorName", floorName);
                houseMap.put("unitName", unitName);
                houseMap.put("houseNo", houseNo);
                houseMap.put("houseName", houseName);
                houseMap.put("houseViewName", houseViewName);
                houseMap.put("type", type);
                houseMap.put("houseType", houseType);
                houseMap.put("avatar", avatar);
                houseMap.put("realname", realname);
                houseMap.put("mobile", mobile);
                houseMap.put("companyNo", companyNo);
                if (mapping.containsKey(communityNo)) {
                    mapping.get(communityNo).add(houseMap);
                } else {
                    List<Map<String, Object>> list = new ArrayList<>();
                    list.add(houseMap);
                    mapping.put(communityNo, list);
                }

                if (!communityMapping.containsKey(communityNo)) {
                    Map<String, Object> info = new LinkedHashMap<>();
                    info.put("id", communityNo);
                    info.put("communityNo", communityNo);
                    info.put("communityName", communityName);
                    info.put("communityViewName", communityViewName);
                    communityMapping.put(communityNo, info);
                }

            }
            communityMapping.forEach((key, value) -> {
                Map<String, Object> communityMap = communityMapping.get(key);
                communityMap.put("childList", mapping.get(key));
                communityMap.put("count", mapping.get(key).size());
                result.add(communityMap);
            });
            return result;
        }
        return result;
    }


}
